/**
 * Created by Amaris on 2016/9/13.
 */

//API接口地址存储文件
var moment = require('moment');
exports.response = {
    logoutSuccess: {
        code: "200",
        msg: "退出成功！"
    },
    loginNo: {
        code: "202",
        msg: "用户未登录！"
    }
};


exports.dateFormat = function (date) {
    return moment(date).format('YYYY-MM-DD HH:mm:ss');
},
exports.getListByActionType = function (type, userJoinList){

    var resultArr=[];
    userJoinList.forEach(function (v, i, a) {

        var actionType=v.actionType;
        var actionId=v.actionId;
        if(type==actionType){
            resultArr.push(actionId);
        }
    })
    return resultArr;
},

exports.getUserJoin = function (userJoinResult, itemArray,uId){

    if(itemArray){
        itemArray.forEach(function (v, i, a) {

            v.userjoin=false;
            if(userJoinResult){

                for( var i = 0;i<userJoinResult.length;i++){

                    var actionId=userJoinResult[i].actionId;
                    var userId=userJoinResult[i].userId;
                    console.log("rowId"+v.rowId +"acitonId"+actionId);
                    if(v.rowId == actionId && uId==userId){
                        v.userjoin=true;
                        userJoinResult.splice(i,1);
                    }
                }
            }

        })
    }

    return itemArray;
},
exports.getSuccessRes = function (data, total) {

    var result = {
        code: "200",
        msg: "success"
    };
    if (!!data) {
        //置为null的不返回
        for (var attr in data) {
            if (data.hasOwnProperty(attr)) {
                if (!data[attr]) {
                    delete data[attr];
                }
            }
        }
        result.obj = data;
    }
    if (!!total) {
        result.total = total;
    }
    return result;
};

exports.getErrorRes = function (code, message) {

    return {
        code: code,
        msg: message
    };

};

exports.getSysError = function(msg){
    var err = new Error();
    err.status = 500;
    err.message = msg || '系统错误';
    return err;
};

exports.getDbError = function(msg){
    var err = new Error();
    err.status = 500;
    err.message = msg || '数据库连接失败';
    return err;
};
